//
//  AgentVC.m
//  代理端
//
//  Created by sliver on 15/12/11.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "AgentVC.h"
#import "Unit.h"
#import "ReportVC.h"
#import "TopupVC.h"
#import "TraderecordVC.h"
#import "ImformationVC.h"

#import "BalanceVC.h"
#import "StockVC.h"
#import "ApplyVC.h"
#import "AboutOurVC.h"
#import "ShoppingMainVC.h"
#import "OrderMainVC.h"
#import "HeadVC.h"
#import "UIImageView+WebCache.h"
@interface AgentVC ()
{
    UIView *mainView; //主页view
    UIView *menuview; //菜单页
}
@property(nonatomic,strong)UILabel *lable;
@end

@implementation AgentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadmenuView];
    [self loadNav];
    [self loadmainView];
}
-(void)loadmainView{
    
    //细条
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 110, ScreenW, 20)];
    //北京
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, ScreenW, 110)];
    backView.backgroundColor = [UIColor whiteColor];
    [mainView addSubview:backView];
    //头像
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(12, 75, 80, 80)];
    NSString *string =  [NSString stringWithFormat:@"%@%@",BASE_URL,USER_IMG];
    NSURL *imgurl = [NSURL URLWithString:string];
    [imgview setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@"代理商信息头像_150.png"]];
    imgview.layer.masksToBounds = YES;
    imgview.layer.cornerRadius = 5;
    imgview.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    imgview.userInteractionEnabled =YES;
    //单击手势
    UITapGestureRecognizer *sigleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signalTapActioninfor)];
    //设置点击手指的根数
    sigleTap.numberOfTouchesRequired = 1;
    //点击屏幕次数
    sigleTap.numberOfTapsRequired = 1;
    [imgview addGestureRecognizer:sigleTap];
    [mainView addSubview:imgview];
    //公司名称
    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(104, 70, 150, 30)];
    nameLab.text = CPOMANY_NAME;
    nameLab.font = [UIFont boldSystemFontOfSize:17];
    [mainView addSubview:nameLab];
    //级别
    UILabel *lavelLab = [[UILabel alloc]initWithFrame:CGRectMake(104, 112, 60, 20)];
    
    switch ([USER_LEVEL integerValue]) {
        case 1:
            lavelLab.text =@"省级代理";
            break;
        case 2:
            lavelLab.text =@"市级代理";
            break;
        case 3:
            lavelLab.text =@"县级代理";
            break;
        case 4:
            lavelLab.text =@"采集代理";
            break;
            
        default:
            break;
    }
    lavelLab.textColor = [UIColor grayColor];
    lavelLab.font = [UIFont systemFontOfSize:15];
    [mainView addSubview:lavelLab];
    
    UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(0, 180, ScreenW, 81)];
    
    view3.backgroundColor = [UIColor whiteColor];
    
    [mainView addSubview:view3];
    
    UIButton *jiantouBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 30, 180, 40 , 40)];
    [jiantouBtn setImage:[UIImage imageNamed:@"回来.png"] forState:UIControlStateNormal];
    [jiantouBtn addTarget:self action:@selector(yueclick) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:jiantouBtn];

    UIButton *butten4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 180, ScreenW, 40)];
    [butten4 addTarget:self action:@selector(yueclick) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:butten4];

    
    
    
    UILabel *yueLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 180, 100, 40)];
    yueLab.text =@"余额";
    yueLab.font = [UIFont systemFontOfSize:17];
    yueLab.textAlignment  = NSTextAlignmentLeft;
    [mainView addSubview:yueLab];
    
    UILabel *yueLabb = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW-200 , 180, 170, 40)];
    CGFloat va = [USER_DIC[@"balance"] floatValue];
    yueLabb.text = [NSString stringWithFormat:@"￥%.2f",va];
//    yueLabb.text = [NSString stringWithFormat:@"￥%@",USER_DIC[@"balance"]];
    yueLabb.textAlignment =NSTextAlignmentRight;
    yueLabb.font = [UIFont boldSystemFontOfSize:17];
    self.lable = yueLabb;
    
    UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(10*KScr,CGRectGetMaxY(yueLabb.frame)+1 , ScreenW-20*KScr, 1);
    
    view2.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    
    [mainView addSubview:view2];
    
    
    [mainView addSubview:yueLabb];
    //充值
    UIButton *chongzhiBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 221, ScreenW, 40)];
    //    [chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"充值"] forState:UIControlStateNormal];
    chongzhiBtn.adjustsImageWhenHighlighted = NO;
    [chongzhiBtn addTarget:self action:@selector(chongzhi) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imagev = [[UIImageView alloc]init];
    imagev.frame = CGRectMake(chongzhiBtn.frame.size.width/2-35,(chongzhiBtn.frame.size.height-15)/2 , 15, 15);
    imagev.image = [UIImage imageNamed:@"btn_chongzhi.png"];
    
    [chongzhiBtn addSubview:imagev];
    
    UILabel *lable = [[UILabel alloc]init];
    lable.frame=CGRectMake(chongzhiBtn.frame.size.width/2-20, 0, chongzhiBtn.frame.size.width/2, 40);
    
    lable.textAlignment = NSTextAlignmentLeft;
    
    lable.text = @"  充值";
    
    lable.font = [UIFont boldSystemFontOfSize:17];
    
    lable.textColor = DRINK_RED_COLOR;
    
    [chongzhiBtn addSubview:lable];
    
    
    [mainView addSubview:chongzhiBtn];
    
    UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(0, 271, ScreenW, 81)];
    
    view4.backgroundColor = [UIColor whiteColor];
    
    [mainView addSubview:view4];

    //库存
    UILabel *yueLab2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 271, ScreenW, 40)];
    yueLab2.text = @"   库存";
    yueLab2.backgroundColor = [UIColor whiteColor];
    yueLab2.font = [UIFont systemFontOfSize:15];
    [mainView addSubview:yueLab2];
    
    UIView *view5 = [[UIView alloc]init];
    view5.frame = CGRectMake(10*KScr,CGRectGetMaxY(yueLab2.frame)+1 , ScreenW-20*KScr, 1);
    
    view5.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    
    [mainView addSubview:view5];

    //按钮
    UIButton *jiantouBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 30, 271, 40 , 40)];
    [jiantouBtn2 setImage:[UIImage imageNamed:@"回来"] forState:UIControlStateNormal];
    [jiantouBtn2 addTarget:self action:@selector(clickkucun) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:jiantouBtn2];
    //库存
    UIButton *kuncunnbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 271, ScreenW, 40)];
    kuncunnbtn.backgroundColor = [UIColor clearColor];
    [kuncunnbtn addTarget:self action:@selector(clickkucun) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:kuncunnbtn];
    
//    //上报库存
    UIButton *shangbaobtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 311, ScreenW, 40)];
//    [shangbaobtn setBackgroundImage:[UIImage imageNamed:@"上报库存"] forState:UIControlStateNormal];
        shangbaobtn.adjustsImageWhenHighlighted = NO;
    [shangbaobtn addTarget:self action:@selector(shangbaokuxun) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *imagev2 = [[UIImageView alloc]init];
    imagev2.frame = CGRectMake(shangbaobtn.frame.size.width/2-50,(shangbaobtn.frame.size.height-20)/2 , 25, 20);
    imagev2.image = [UIImage imageNamed:@"kucunxin.png"];
    
    [shangbaobtn addSubview:imagev2];
    
    UILabel *lable2 = [[UILabel alloc]init];
    lable2.frame=CGRectMake(shangbaobtn.frame.size.width/2-25, 0, shangbaobtn.frame.size.width/2, 40);
    
    lable2.textAlignment = NSTextAlignmentLeft;
    
    lable2.text = @"  上报库存";
    
    lable2.font = [UIFont boldSystemFontOfSize:17];
    
    lable2.textColor = DRINK_RED_COLOR;
    
    [shangbaobtn addSubview:lable2];
    

    [mainView addSubview:shangbaobtn];
//    //上报库现存、
//    UILabel *shangbaolab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW/2-12, 282, 80, 25)];
//    shangbaolab.text = @"上报库存";
//    shangbaolab.backgroundColor = [UIColor whiteColor];
//    shangbaolab.font = [UIFont boldSystemFontOfSize:16];
//    [mainView addSubview:shangbaolab];
//    
//     //交易纪律
    UILabel *jiaoyiLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 361, ScreenW, 40)];
    jiaoyiLab.text = @"   交易记录";
    jiaoyiLab.backgroundColor = [UIColor whiteColor];
    jiaoyiLab.font = [UIFont systemFontOfSize:15];
    [mainView addSubview:jiaoyiLab];
    //箭头
    UIButton *jiantouBtn4 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 30, 361, 40 , 40)];
    [jiantouBtn4 setImage:[UIImage imageNamed:@"回来"] forState:UIControlStateNormal];
    [jiantouBtn4 addTarget:self action:@selector(jiaoyijilv) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:jiantouBtn4];
//交易记录
    UIButton *jiaoyibtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 361, ScreenW, 40)];
    jiaoyibtn.backgroundColor = [UIColor clearColor];
    [jiaoyibtn addTarget:self action:@selector(jiaoyijilv) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:jiaoyibtn];
    //申请返利
    UILabel *shenqingLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 402, ScreenW, 40)];
    shenqingLab.text = @"   申请返利";
    shenqingLab.backgroundColor = [UIColor whiteColor];
    shenqingLab.font = [UIFont systemFontOfSize:15];
    [mainView addSubview:shenqingLab];
    //箭头
    UIButton *jiantouBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 30, 402, 40, 40)];
    [jiantouBtn3 setImage:[UIImage imageNamed:@"回来"] forState:UIControlStateNormal];
    [jiantouBtn3 addTarget:self action:@selector(shenqingfanli) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:jiantouBtn3];
    UIButton *shenqingBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 402, ScreenW, 40)];
    shenqingBtn.backgroundColor = [UIColor clearColor];
    [shenqingBtn addTarget:self action:@selector(shenqingfanli) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:shenqingBtn];
}
//-(void)dismmss
//{
//    NSLog(@"123456");
//
//}
#pragma mark - 库存
-(void)clickkucun{
    StockVC *stockvc = [[StockVC alloc]init];
    stockvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    stockvc.fromto =fromTypeToHome;
    [self presentViewController:stockvc animated:YES completion:nil];
}
#pragma mark - 充值
-(void)chongzhi{
    TopupVC *topupvc = [[TopupVC alloc]init];
    topupvc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:topupvc animated:YES completion:nil];

}
#pragma mark - 上报库存
-(void)shangbaokuxun{
    ReportVC *reportvc = [[ReportVC alloc]init];
     reportvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:reportvc animated:YES completion:nil];
}
#pragma mark - 余额
-(void)yueclick{
    BalanceVC *banlancevc = [[BalanceVC alloc]init];
      banlancevc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    banlancevc.fromto = fromTypeTohome;
    [self presentViewController:banlancevc animated:YES completion:nil];
}
#pragma mark- 交易纪律
-(void)jiaoyijilv{
    TraderecordVC *tradetvc = [[TraderecordVC alloc]init];
    tradetvc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:tradetvc animated:YES completion:nil];
}
#pragma mark - 申请返利
-(void)shenqingfanli{
    ApplyVC *applyvc =[[ApplyVC alloc]init];
    applyvc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
    applyvc.fromto = fromtypetoHome;
    [self presentViewController:applyvc animated:YES completion:nil];

}
#pragma mark - 代理商信息
-(void)signalTapAction{
    [self dismissViewControllerAnimated:NO completion:nil];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"AgentVC" forKey:@"vc"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ceshi" object:nil userInfo:dic];
}
#pragma mark - 代理商信息
-(void)signalTapActioninfor{
    ImformationVC *agentvc = [[ImformationVC alloc]init];
    agentvc.modalTransitionStyle =     UIModalTransitionStyleCoverVertical;
    [self presentViewController:agentvc animated:YES completion:nil];
}

#pragma mark - nav
-(void)loadNav{
    mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    mainView.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [self.view addSubview:mainView];
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [mainView addSubview:redview];
    
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"代理商主页";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:17];
    [mainView addSubview:redLab];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20,44, 40)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setBackgroundImage:[UIImage imageNamed:@"菜单"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:backBtn];
}
-(void)viewWillAppear:(BOOL)animated
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:USER_ACCOUNT forKey:@"agency_id_"];
    [MyMbd addHudToView:self.view];
    [WXDataService requestWithURL:@"mobile/agency/getAgencyInfo.jhtml" params:dic httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        //    self.block();
//        NSLog(@"%@",result);
        [MyMbd hidenHudFromView:self.view];
        NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
        [userDefaultes setObject:result[@"agencyPO"] forKey:@"USER_DIC"];
        [userDefaultes synchronize];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeValue" object:nil];
        
    } failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark - 菜单页
-(void)loadmenuView{
    menuview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW *0.75, ScreenH)];
    menuview.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    for(int i=1;i<9;i++){
        NSArray *menuAry = @[@"    首页",@"    商城",@"    订单",@"    余额",@"    库存",@"    申请返利",@"    关于我们",@"    退出"];
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100+41*i, ScreenW *0.75, 40)];
        btn.tag = i;
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:[menuAry objectAtIndex:i-1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickmenu:) forControlEvents:UIControlEventTouchUpInside];
        [menuview addSubview:btn];
    }
    //上面头像等
    UIImageView*imageview = [[UIImageView alloc]init];
    imageview.backgroundColor = [UIColor whiteColor];
    NSString *string =  [NSString stringWithFormat:@"%@%@",BASE_URL,USER_IMG];
    NSURL *imgurl = [NSURL URLWithString:string];
    [imageview setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@"代理商信息头像_150.png"]];
    imageview.frame =CGRectMake(20, 40, 80, 80);
    imageview.layer.masksToBounds = YES;
    imageview.layer.cornerRadius = 5;
    imageview.userInteractionEnabled = YES;
    //单击手势
    UITapGestureRecognizer *iconSigleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signalTapAction)];
    [imageview addGestureRecognizer:iconSigleTap];
    [menuview addSubview:imageview];
    
    //名称
    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(120, 40, ScreenW *0.75 - 120, 20)];
    nameLab.text = CPOMANY_NAME;
    nameLab.font = [UIFont systemFontOfSize:15];
    [menuview addSubview:nameLab];
    //等级
    UILabel *levelLab = [[UILabel alloc]initWithFrame:CGRectMake(120, 65, ScreenW *0.75 - 120, 20)];
    levelLab.font = [UIFont systemFontOfSize:15];
    switch ([USER_LEVEL integerValue]) {
        case 1:
            levelLab.text =@"省级代理";
            break;
        case 2:
            levelLab.text =@"市级代理";
            break;
        case 3:
            levelLab.text =@"县级代理";
            break;
        case 4:
            levelLab.text =@"采集代理";
            break;
            
        default:
            break;
    }
    [menuview addSubview:levelLab];

    //中间竖线
    UIView *vieww = [[UIView alloc]initWithFrame:CGRectMake(ScreenW *0.75-1, 0, 1, ScreenH)];
    vieww.backgroundColor = [UIColor blackColor];
    vieww.layer.shadowColor = [[UIColor blackColor]CGColor];
    vieww.layer.shadowRadius = 3;
    vieww.layer.shadowOpacity = 0.9;
    vieww.layer.shadowOffset = CGSizeMake(-3, 0);
    [menuview addSubview:vieww];
    [self.view addSubview:menuview];
}
#pragma mark - 点击菜单
-(void)clickmenu:(UIButton *)sender{
    NSArray *array = @[@"HeadVC",@"ShoppingMainVC",@"OrderMainVC",@"BalanceVC",@"StockVC",@"ApplyVC",@"AboutOurVC",@"LandingVC"];
    if (sender.tag < 9) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{@"vc":array[sender.tag-1]};
        
//        NSLog(@"%@",dic);
        
        if (sender.tag-1==0) {
            [self dismissViewControllerAnimated:NO completion:^{}];
        }else{
            if (sender.tag == 8) {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"tuichudenglu"];
            }
            [self dismissViewControllerAnimated:NO completion:^{}];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"ceshi" object:self userInfo:dic];
        }
    }else{
        AppDelegate *dele = (AppDelegate *)[[UIApplication sharedApplication]delegate] ;
        NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
        [userDefaultes setObject: nil forKey:@"USER_ID"];
        [userDefaultes setObject:nil forKey:@"USER_PWD"];
        [userDefaultes setObject:nil forKey:@"USER_IMG"];
        [userDefaultes setObject:nil forKey:@"USER_LEVEL"];
        [userDefaultes setObject:nil forKey:@"USER_ACCOUNT"];
        [userDefaultes setObject:nil forKey:@"USER_DIC"];
        [userDefaultes setObject:nil forKey:@"CPOMANY_NAME"];
        [userDefaultes synchronize];
        [dele showMain];
    }
}
-(void)viewTapAction{
    [UIView animateWithDuration:0.2 animations:^(){
        mainView.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    }];
}
#pragma mark - 菜单按钮
-(void)clickBack:(UIButton *)sender{
    sender.selected = !sender.selected;
    if(sender.selected){
        [UIView animateWithDuration:0.2 animations:^(){
            mainView.frame = CGRectMake(ScreenW*0.75, 0, ScreenW, ScreenH);
        }];
        
        UIView *shoushiView = [[UIView alloc]initWithFrame:CGRectMake(ScreenW*0.75, 0, ScreenW*0.25, ScreenH)];
        shoushiView.tag = 1999;
        shoushiView.userInteractionEnabled = YES;
        shoushiView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:shoushiView];
        //单击手势
        UITapGestureRecognizer *sigleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapAction)];
        //设置点击手指的根数
        sigleTap.numberOfTouchesRequired = 1;
        //点击屏幕次数
        sigleTap.numberOfTapsRequired = 1;
        [shoushiView addGestureRecognizer:sigleTap];
    }else{
        UIView *view = (UIView *)[self.view viewWithTag:1999];
        [view removeFromSuperview];
        [UIView animateWithDuration:0.2 animations:^(){
            mainView.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        }];
    }
}

@end
